package br.com.tbp.gui.table;

import java.awt.Component;
import java.math.BigDecimal;
import java.util.Date;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;

/**
 * 
 * 
 * @author Arthur Gregorio
 * @version 1.0.0
 * @since Release 1.0.0
 */
public class JTableUtils {

    /**
     * Método que ajuasta a coluna conforme da JTable conforme o tamanho do
     * conteúdo carregado no model
     *
     * @param coluna
     *            Recebe a coluna que sera redimensionada
     * @param margin
     *            espaço de bonus colocado caso a contagem dos campos esteja
     *            errada
     * @param tabela
     *            tabela que será implementada o redimensionamento
     */
    public static void fitColumn(int coluna, int margin, JTable tabela) {
        int width = 0;

        TableColumnModel colModel = tabela.getColumnModel();
        TableColumn col = colModel.getColumn(coluna);

        /** Obtem a largura do cabecalho */
        TableCellRenderer renderer = col.getHeaderRenderer();

        if (renderer == null) {
            renderer = tabela.getTableHeader().getDefaultRenderer();
        }

        Component comp = renderer.getTableCellRendererComponent(tabela, col.getHeaderValue(), false, false, 0, 0);
        width = comp.getPreferredSize().width;

        for (int r = 0; r < tabela.getRowCount(); r++) {
            renderer = tabela.getCellRenderer(r, coluna);
            comp = renderer.getTableCellRendererComponent(tabela, tabela.getValueAt(r, coluna), false, false, r, coluna);
            width = Math.max(width, comp.getPreferredSize().width);
        }

        width += 2 * margin;
        col.setPreferredWidth(width);
    }

    public static void fitAllColumnsWithoutRenderer(JTable table) {
        TableColumnModel colModel = table.getColumnModel();
        for (int i = 0; i < colModel.getColumnCount(); i++) {
            fitColumn(i, 0, table);
        }
    }

    public static void fitAllColumns(JTable table) {
        TableColumnModel colModel = table.getColumnModel();
        for (int i = 0; i < colModel.getColumnCount(); i++) {
            fitColumn(i, 0, table);
        }

        rendererAllColumns(table);
    }

    public static void fitAllColumns(JTable table, DefaultTableCellRenderer rendererTable) {
        TableColumnModel colModel = table.getColumnModel();
        for (int i = 0; i < colModel.getColumnCount(); i++) {
            fitColumn(i, 0, table);
        }

        rendererAllColumns(table, rendererTable);
    }

    public static void rendererAllColumns(JTable table) {
        DefaultTableRenderer defaultTableRenderer = new DefaultTableRenderer();
        table.setDefaultRenderer(Object.class, defaultTableRenderer);
        table.setDefaultRenderer(Integer.class, defaultTableRenderer);
        table.setDefaultRenderer(BigDecimal.class, defaultTableRenderer);
        table.setDefaultRenderer(Long.class, defaultTableRenderer);
        table.setDefaultRenderer(Double.class, defaultTableRenderer);
        table.setDefaultRenderer(Date.class, defaultTableRenderer);
        table.setDefaultRenderer(Boolean.class, new CheckBoxTableRenderer());
    }

    public static void rendererAllColumns(JTable table, DefaultTableCellRenderer rendererTable) {
        table.setDefaultRenderer(Object.class, rendererTable);
        table.setDefaultRenderer(Integer.class, rendererTable);
        table.setDefaultRenderer(Object.class, rendererTable);
        table.setDefaultRenderer(BigDecimal.class, rendererTable);
        table.setDefaultRenderer(Long.class, rendererTable);
        table.setDefaultRenderer(Double.class, rendererTable);
        table.setDefaultRenderer(Date.class, rendererTable);
        table.setDefaultRenderer(Boolean.class, new CheckBoxTableRenderer());
    }
}
